package org.jivesoftware.smack.sasl;

import de.measite.smack.Sasl;
import java.io.IOException;
import java.util.HashMap;
import org.apache.harmony.javax.security.auth.callback.Callback;
import org.apache.harmony.javax.security.auth.callback.CallbackHandler;
import org.apache.harmony.javax.security.auth.callback.NameCallback;
import org.apache.harmony.javax.security.auth.callback.PasswordCallback;
import org.apache.harmony.javax.security.auth.callback.UnsupportedCallbackException;
import org.apache.harmony.javax.security.sasl.RealmCallback;
import org.apache.harmony.javax.security.sasl.RealmChoiceCallback;
import org.apache.harmony.javax.security.sasl.SaslClient;
import org.apache.harmony.javax.security.sasl.SaslException;
import org.jivesoftware.smack.SASLAuthentication;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.util.StringUtils;

public abstract class SASLMechanism
  implements CallbackHandler
{
  private SASLAuthentication saslAuthentication;
  protected SaslClient sc;
  protected String authenticationId;
  protected String password;
  protected String hostname;

  public SASLMechanism(SASLAuthentication paramSASLAuthentication)
  {
    this.saslAuthentication = paramSASLAuthentication;
  }

  public void authenticate(String paramString1, String paramString2, String paramString3)
    throws IOException, XMPPException
  {
    this.authenticationId = paramString1;
    this.password = paramString3;
    this.hostname = paramString2;
    String[] arrayOfString = { getName() };
    HashMap localHashMap = new HashMap();
    this.sc = Sasl.createSaslClient(arrayOfString, paramString1, "xmpp", paramString2, localHashMap, this);
    authenticate();
  }

  public void authenticate(String paramString1, String paramString2, CallbackHandler paramCallbackHandler)
    throws IOException, XMPPException
  {
    String[] arrayOfString = { getName() };
    HashMap localHashMap = new HashMap();
    this.sc = Sasl.createSaslClient(arrayOfString, paramString1, "xmpp", paramString2, localHashMap, paramCallbackHandler);
    authenticate();
  }

  protected void authenticate()
    throws IOException, XMPPException
  {
    String str = null;
    try
    {
      if (this.sc.hasInitialResponse())
      {
        byte[] arrayOfByte = this.sc.evaluateChallenge(new byte[0]);
        str = StringUtils.encodeBase64(arrayOfByte, false);
      }
    }
    catch (SaslException localSaslException)
    {
      throw new XMPPException("SASL authentication failed", localSaslException);
    }
    getSASLAuthentication().send(new AuthMechanism(getName(), str));
  }

  public void challengeReceived(String paramString)
    throws IOException
  {
    byte[] arrayOfByte;
    if (paramString != null)
      arrayOfByte = this.sc.evaluateChallenge(StringUtils.decodeBase64(paramString));
    else
      arrayOfByte = this.sc.evaluateChallenge(new byte[0]);
    Response localResponse;
    if (arrayOfByte == null)
      localResponse = new Response();
    else
      localResponse = new Response(StringUtils.encodeBase64(arrayOfByte, false));
    getSASLAuthentication().send(localResponse);
  }

  protected abstract String getName();

  protected SASLAuthentication getSASLAuthentication()
  {
    return this.saslAuthentication;
  }

  public void handle(Callback[] paramArrayOfCallback)
    throws IOException, UnsupportedCallbackException
  {
    for (int i = 0; i < paramArrayOfCallback.length; i++)
    {
      Object localObject;
      if ((paramArrayOfCallback[i] instanceof NameCallback))
      {
        localObject = (NameCallback)paramArrayOfCallback[i];
        ((NameCallback)localObject).setName(this.authenticationId);
      }
      else if ((paramArrayOfCallback[i] instanceof PasswordCallback))
      {
        localObject = (PasswordCallback)paramArrayOfCallback[i];
        ((PasswordCallback)localObject).setPassword(this.password.toCharArray());
      }
      else if ((paramArrayOfCallback[i] instanceof RealmCallback))
      {
        localObject = (RealmCallback)paramArrayOfCallback[i];
        ((RealmCallback)localObject).setText(this.hostname);
      }
      else if (!(paramArrayOfCallback[i] instanceof RealmChoiceCallback))
      {
        throw new UnsupportedCallbackException(paramArrayOfCallback[i]);
      }
    }
  }

  public class AuthMechanism extends Packet
  {
    private final String name;
    private final String authenticationText;

    public AuthMechanism(String paramString1, String arg3)
    {
      if (paramString1 == null)
        throw new NullPointerException("SASL mechanism name shouldn't be null.");
      this.name = paramString1;
      Object localObject;
      this.authenticationText = localObject;
    }

    public String toXML()
    {
      StringBuilder localStringBuilder = new StringBuilder();
      localStringBuilder.append("<auth mechanism=\"").append(this.name);
      localStringBuilder.append("\" xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
      if ((this.authenticationText != null) && (this.authenticationText.trim().length() > 0))
        localStringBuilder.append(this.authenticationText);
      localStringBuilder.append("</auth>");
      return localStringBuilder.toString();
    }
  }

  public static class Challenge extends Packet
  {
    private final String data;

    public Challenge(String paramString)
    {
      this.data = paramString;
    }

    public String toXML()
    {
      StringBuilder localStringBuilder = new StringBuilder();
      localStringBuilder.append("<challenge xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
      if ((this.data != null) && (this.data.trim().length() > 0))
        localStringBuilder.append(this.data);
      localStringBuilder.append("</challenge>");
      return localStringBuilder.toString();
    }
  }

  public static class Failure extends Packet
  {
    private final String condition;

    public Failure(String paramString)
    {
      this.condition = paramString;
    }

    public String getCondition()
    {
      return this.condition;
    }

    public String toXML()
    {
      StringBuilder localStringBuilder = new StringBuilder();
      localStringBuilder.append("<failure xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
      if ((this.condition != null) && (this.condition.trim().length() > 0))
        localStringBuilder.append("<").append(this.condition).append("/>");
      localStringBuilder.append("</failure>");
      return localStringBuilder.toString();
    }
  }

  public class Response extends Packet
  {
    private final String authenticationText;

    public Response()
    {
      this.authenticationText = null;
    }

    public Response(String arg2)
    {
      Object localObject;
      if ((localObject == null) || (localObject.trim().length() == 0))
        this.authenticationText = null;
      else
        this.authenticationText = localObject;
    }

    public String toXML()
    {
      StringBuilder localStringBuilder = new StringBuilder();
      localStringBuilder.append("<response xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
      if (this.authenticationText != null)
        localStringBuilder.append(this.authenticationText);
      localStringBuilder.append("</response>");
      return localStringBuilder.toString();
    }
  }

  public static class Success extends Packet
  {
    private final String data;

    public Success(String paramString)
    {
      this.data = paramString;
    }

    public String toXML()
    {
      StringBuilder localStringBuilder = new StringBuilder();
      localStringBuilder.append("<success xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
      if ((this.data != null) && (this.data.trim().length() > 0))
        localStringBuilder.append(this.data);
      localStringBuilder.append("</success>");
      return localStringBuilder.toString();
    }
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     org.jivesoftware.smack.sasl.SASLMechanism
 * JD-Core Version:    0.6.1
 */